﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using DevExpress.XtraEditors;

namespace Speedy
{
    public enum ListeTipleri : byte
    {
        CariKodu = 0,
        CariAdi = 1,
        StokKodu = 2,
        StokAdi = 3,
        Barkod = 4,
        Terminal = 5
    }

    public partial class frmListe : XtraForm
    {
        public frmListe()
        {
            InitializeComponent();
        }

        public ListeTipleri ListeTipi { get; set; }
        public string Filtre { get; set; }
        public string[] Value { get; set; }

        void ShowList()
        {
            dbListe liste = new dbListe();
            switch (ListeTipi)
            {
                case ListeTipleri.CariKodu:
                    this.Text = "Cari Listesi";
                    gridControl1.DataSource = liste.LoadData(ListeTipleri.CariKodu, this.Filtre);
                    break;
                case ListeTipleri.CariAdi:
                    this.Text = "Cari Listesi";
                    gridControl1.DataSource = liste.LoadData(ListeTipleri.CariAdi, this.Filtre);
                    break;
                case ListeTipleri.StokKodu:
                    this.Text = "Ürün Listesi";
                    gridControl1.DataSource = liste.LoadData(ListeTipleri.StokKodu, this.Filtre);
                    break;
                case ListeTipleri.StokAdi:
                    this.Text = "Ürün Listesi";
                    gridControl1.DataSource = liste.LoadData(ListeTipleri.StokAdi, this.Filtre);
                    break;
                case ListeTipleri.Barkod:
                    this.Text = "Ürün Listesi";
                    gridControl1.DataSource = liste.LoadData(ListeTipleri.Barkod, this.Filtre);
                    break;
                case ListeTipleri.Terminal:
                    this.Text = "Terminal Listesi";
                    gridControl1.DataSource = liste.LoadData(ListeTipleri.Terminal, this.Filtre);
                    gridView1.Columns["DEVICE_CODE"].Visible = false;
                    gridView1.Columns["LICENCE_CODE"].Visible = false;
                    break;
                default:
                    break;
            }
            gridView1.BestFitColumns();
            gridView1.Focus();
        }

        private void frmListe_Load(object sender, EventArgs e)
        {
            this.ShowList();
        }

        private void gridView1_DoubleClick(object sender, EventArgs e)
        {
            if (gridView1.FocusedRowHandle > -1)
            {
                List<string> val = new List<string>();
                foreach (DevExpress.XtraGrid.Columns.GridColumn col in this.gridView1.Columns)
                    val.Add(gridView1.GetRowCellValue(gridView1.FocusedRowHandle, col).ToString());
                this.Value = val.ToArray();
                DialogResult = DialogResult.OK;
                this.Close();
            }
        }

        private void gridView1_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.KeyCode == Keys.Enter)
            {
                if (gridView1.FocusedRowHandle > -1)
                {
                    List<string> val = new List<string>();
                    foreach (DevExpress.XtraGrid.Columns.GridColumn col in this.gridView1.Columns)
                        val.Add(gridView1.GetRowCellValue(gridView1.FocusedRowHandle, col).ToString());
                    this.Value = val.ToArray();
                    DialogResult = DialogResult.OK;
                    this.Close();
                }
            }
            else if (e.KeyCode == Keys.Escape)
            {
                DialogResult = DialogResult.Abort;
                this.Close();
            }
        }

        private void frmListe_Activated(object sender, EventArgs e)
        {
            gridView1.Focus();
        }

    }
}
